package com.wuyou.simple;

/**
 * 1779. 找到最近的有相同 X 或 Y 坐标的点
 * 两个点 (x1, y1) 和 (x2, y2) 之间的 曼哈顿距离 为 abs(x1 - x2) + abs(y1 - y2) 。
 */
public class NearestValidPoint {


    public static void main(String[] args) {
        int x = 1, y = 1;
        int[][] points = {{1, 1}, {1, 1}};
        System.out.println(solution_1(x, y, points));
    }

    public static int solution_1(int x, int y, int[][] points) {

        int minDistance = 0, resIndex = -1;
        for (int i = 0; i < points.length; i++) {
            if (points[i][0] == x || points[i][1] == y) {
                int tempDistance = Math.abs(points[i][0] - x) + Math.abs(points[i][1] - y);
                if (resIndex == -1) {
                    minDistance = tempDistance;
                    resIndex = i;
                    continue;
                }
                if (minDistance == tempDistance) continue;
                if (tempDistance < minDistance) {
                    minDistance = tempDistance;
                    resIndex = i;
                }
            }
        }
        return resIndex;
    }
}
